package com.wushijia.classload;

import com.wushijia.model.User;
import org.springframework.core.ResolvableType;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 通过 Java 反射从对应的Class类型中来获取信息
 */
public class ResolvableTypeTest {
    private HashMap<Integer, List<String>> map;
    public static void main(String[] args) throws NoSuchFieldException {
        ResolvableType resolvableType1 = ResolvableType.forClass(String.class);
        ResolvableType resolvableType2 = ResolvableType.forClass(Map.class, HashMap.class);
        User user = new User("张三",20);
        Constructor<?> constructor = user.getClass().getConstructors()[0];
        // java.lang.String
        System.out.println(ResolvableType.forConstructorParameter(constructor,0));
        Field field = ResolvableTypeTest.class.getDeclaredField("map");
        // java.util.HashMap<java.lang.String, java.util.List<java.lang.Object>>
        ResolvableType resolvableType = ResolvableType.forField(field);
        resolvableType.getSuperType(); // AbstractMap<Integer, List<String>>
        resolvableType.asMap(); // Map<Integer, List<String>>
        resolvableType.getGeneric(0).resolve(); // Integer
        resolvableType.getGeneric(1).resolve(); // List
        resolvableType.getGeneric(1); // List<String>
        resolvableType.resolveGeneric(1, 0); // String
        System.out.println(resolvableType);


        Method method = user.getClass().getMethods()[4];
        // java.lang.String
        System.out.println(ResolvableType.forMethodParameter(method,0));

        ResolvableType resolvableType3 = ResolvableType.forClass(String.class);
        // java.lang.String []
        ResolvableType arrayComponent = ResolvableType.forArrayComponent(resolvableType);
        System.out.println(arrayComponent);
    }
}
